﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;


namespace MusicStore.Silverlight.MusicStoreService
{
   public partial class Album 
   {
      public Album()
      {
         Tracks = new System.Collections.ObjectModel.ObservableCollection<Track>();
      }

      #region "exception validation methods"

		partial void ValidateProperty(string propertyName, object value)
		{
			// Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = propertyName });
			switch (propertyName)
			{
				case "Title":
					if (!IsRequired(value))
						throw new SystemException("A Title is Required");
					break;
				case "PublicationDate":
					if (!IsRequired(value))
						throw new SystemException("A Pubblication Date is Required");
					if (!IsDateBetween(value, new DateTime(1900, 01, 01), DateTime.Now))
						throw new SystemException(string.Format("The Pubblication Date must be between {0:yyyy MM dd} and {1:yyyy MM dd}", new DateTime(1900, 01, 01), DateTime.Now));
					break;
			}
		}

      /// <summary>
      /// title must be non empty
      /// </summary>
      /// <param name="value"></param>
      /// <returns></returns>
      private static bool IsRequired(object value)
      {
         if (value == null) return false;
			
			if (value is string)
				return !string.IsNullOrEmpty(value.ToString());
      	
			return true;
      }

      /// <summary>
      /// publication date must be greated than 1900
      /// </summary>
      /// <param name="value"></param>
      /// <param name="startdate"></param>
      /// <param name="enddate"></param>
      /// <returns></returns>
      private static bool IsDateBetween(object value, DateTime startdate, DateTime enddate)
      {
      	DateTime date = Convert.ToDateTime(value);
         if (date < startdate || date > enddate)
				return false;
      	return true;
      }

      #endregion

      #region "Validation with data annotation"

      /// <summary>
      /// let's extend our business object with a validation suitable property
      /// </summary>
      [Required(ErrorMessage="A Title is Required!")]
      public string ValidatableTitle
      {
         get { return Title; }
         set
         {
            AnnotationValidateProperty("ValidatableTitle", value);

            if ((ReferenceEquals(Title, value) != true))
            {
               Title = value;
               RaisePropertyChanged("ValidatableTitle");
            }
         }
      }

      private void AnnotationValidateProperty(string propertyName, object value)
      {
         var ctx = new ValidationContext(this, null, null);
         ctx.MemberName = propertyName;
         // If fails, throws an excption
         Validator.ValidateProperty(value, ctx);
      }

      #endregion
	}
}
